﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Basic4Net.BusinessRules.View;
using Basic4Net.Entities;
using Microsoft.Data.Extensions;

namespace Basic4Net.BusinessRules
{
    public class Lesson
    {
        private static Lesson _lessonInstance;
        public static Lesson LessonInstance
        {
            get { return _lessonInstance ?? (_lessonInstance = new Lesson()); }
        }



        public IList<TeachLessonList> FindLessonByTeachPost(int startAt, int step, string teachCode, string schoolMark, int? postId,int year, int month,ref int count)
        {
            using (DBEntities db = new DBEntities())
            {
                var lessonTemp = (from t in db.t_Lesson
                                  where t.FYear == year && t.FMonth == month
                                  select new
                                  {
                                      TeacherID = (int)t.TeacherID,
                                      LessonNum = (double)t.Lesson,
                                      Year = (int)t.FYear,
                                      Month = (int)t.FMonth,
                                      LessonID = t.RID
                                  });
                var teachTemp = (from t in db.TEACHERINFO
                                 join lesson in lessonTemp on t.RID equals lesson.TeacherID into temp
                                 from result in temp.DefaultIfEmpty()
                                 where t.SCHOOLMARK == schoolMark && t.ISEFFECT == 1
                                      && (string.IsNullOrEmpty(teachCode)
                                           ? true
                                           : teachCode == "" ? true : (t.TEACHCODE.Contains(teachCode) || t.TEACHNAME.Contains(teachCode)))
                                 select new
                                            {
                                                TeacherID = t.RID,
                                                TeachName = t.TEACHNAME,
                                                TeachCode = t.TEACHCODE,
                                                Sexy = t.SEXY == null ? string.Empty : t.SEXY == 0 ? "男" : "女",
                                                BirthDay = t.BIRTHDAY == null ? DateTime.MaxValue : (DateTime)t.BIRTHDAY,
                                                LessonNum = (double)result.LessonNum,
                                                Year = (int)result.Year,
                                                Month = (int)result.Month,
                                                LessonID = result.LessonID
                                            });

                string s = teachTemp.ToTraceString();
                var postList = from p in db.t_TeacherType
                               join pr in db.t_TeacherRelation on p.RID equals pr.TypeID into temp
                               from result in temp
                               where p.SchoolMark == schoolMark && result.SchoolMark == schoolMark
                               select new
                                          {
                                              PostId = p.RID,
                                              PostName = p.TypeName,
                                              TeachId = result.TeacherID
                                          };
                 
                var resultList = from t in teachTemp
                                 join p in postList on t.TeacherID equals p.TeachId
                                 into temp
                                 from result in temp
                                 where result.PostId == postId
                                 orderby t.TeachCode descending
                                 select new TeachLessonList
                                            {
                                                TeacherID = t.TeacherID,
                                                TeachName = t.TeachName,
                                                TeachCode = t.TeachCode,
                                                Sexy = t.Sexy,
                                                BirthDay = t.BirthDay,
                                                PostID = result.PostId,
                                                PostName = result.PostName,
                                                Lesson = t.LessonNum==null?0:t.LessonNum,
                                                Year = t.Year==null?0:t.Year,
                                                Month = t.Month ==null?0:t.Month,
                                                LessonID = t.LessonID==null?0:t.LessonID
                                            };
                count = resultList.Count();
                IList<TeachLessonList> resList = resultList.Skip(startAt).Take(step).ToList<TeachLessonList>();
                return resList; 
            }
        }

        /// <summary>
        /// 添加课时信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool AddTeachLesson(t_Lesson entity)
        {
            bool result = true;
            using (DBEntities db = new DBEntities())
            {
                var b = from l in db.t_Lesson
                        where l.FYear == entity.FYear && l.FMonth == entity.FMonth && l.TeacherID == entity.TeacherID
                        select l;
                if (b.Count() > 0)
                    return false;
                db.AddTot_Lesson(entity);
                try
                {
                    db.SaveChanges();
                }
                catch (Exception)
                {
                    result = false; 
                }
            }
            return result;
        }


        /// <summary>
        /// 修改课时信息
        /// </summary>
        /// <param name="lessonId"></param>
        /// <param name="lessonNum"></param>
        /// <returns></returns>
        public bool UpdateTeachLesson(int lessonId, double lessonNum)
        {
            bool result = true;
            if(lessonId <= 0)
                return false;
            using (DBEntities db = new DBEntities())
            {
                var temp = from b in db.t_Lesson
                           where b.RID == lessonId
                           select b;

                if(temp.Count() > 0)
                {
                    foreach (var tLesson in temp)
                    {
                        tLesson.Lesson = lessonNum;
                    }
                    try
                    {
                        db.SaveChanges();
                    }
                    catch (Exception)
                    {
                        result = false; 
                    }
                }
            }
            return result;
        }



       
    }
}
